package com.qianchang.microfinance.view.mine;

import android.app.AlertDialog;
import android.content.DialogInterface;
import android.os.Bundle;
import android.text.TextPaint;
import android.text.method.LinkMovementMethod;
import android.text.style.ClickableSpan;
import android.view.View;
import android.widget.Button;

import com.bigkoo.pickerview.model.IPickerViewData;
import com.dm.library.http.DMOKHttpUtils;
import com.dm.library.http.HttpParams;
import com.dm.library.http.base.JsonResult;
import com.dm.library.http.callback.RequestCallBack;
import com.dm.library.utils.StringUtils;
import com.dm.library.utils.ToastUtil;
import com.dm.library.widgets.ClearEditText;
import com.dm.library.widgets.OneLineLinearLayout;
import com.dm.library.widgets.element.DButton;
import com.qianchang.microfinance.R;
import com.qianchang.microfinance.bean.FindUserBasicInfoBean;
import com.qianchang.microfinance.bean.LoanType;
import com.qianchang.microfinance.bean.LoanUsageBean;
import com.qianchang.microfinance.bean.Product;
import com.qianchang.microfinance.bean.ProductBean;
import com.qianchang.microfinance.bean.ProductConfigBean;
import com.qianchang.microfinance.bean.ProjectStatus;
import com.qianchang.microfinance.bean.SubmitLoanBean;
import com.qianchang.microfinance.bean.UserLoginInfo;
import com.qianchang.microfinance.bean.UserSupplBean;
import com.qianchang.microfinance.bean.event.AgreementTitleBean;
import com.qianchang.microfinance.global.ConstantAppInfo;
import com.qianchang.microfinance.global.DMApplication;
import com.qianchang.microfinance.utils.DMConstant;
import com.qianchang.microfinance.utils.SpanUtils;
import com.qianchang.microfinance.view.base.BaseActivity;
import com.qianchang.microfinance.view.dialog.CommonDialog;
import com.qianchang.microfinance.view.home.cash.ApplyCashActivity;
import com.qianchang.microfinance.view.mine.bill.PayBackActivity;
import com.qianchang.microfinance.view.mine.loan.ConfirmAgreementActivity;
import com.qianchang.microfinance.view.mine.loan.LoanProgressActivity;
import com.qianchang.microfinance.view.product.ProductFragment;
import com.qianchang.microfinance.view.register.AgreementActivity;
import com.qianchang.microfinance.view.register.AgreementApi;

import org.json.JSONArray;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

public class UserSupplActivity extends BaseActivity implements CommonDialog.OnItemSelectListener {

    private List<ProductConfigBean.RepaymentTypeBean> imcomeList;
    private List<ProductConfigBean.RepaymentTypeBean> unitList;
    private List<ProductConfigBean.RepaymentTypeBean> workList;

    @BindView(R.id.et_input_email)
    ClearEditText etEamil;
    @BindView(R.id.et_input_address)
    ClearEditText etAddress;
    @BindView(R.id.oll_income)
    OneLineLinearLayout ollIncome;
    @BindView(R.id.oll_unit_type)
    OneLineLinearLayout ollUnitType;
    @BindView(R.id.oll_work_type)
    OneLineLinearLayout ollWorkType;
    @BindView(R.id.btn_user_suppl_ok)
    Button btnCommit;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_user_suppl);
        ButterKnife.bind(this);
        initView();
        initData();
        reset();
        loanUserSuppl();
    }

    @Override
    protected void initView() {
        super.initView();
        setTitleName(getString(R.string.title_my_suppl));
    }

    @Override
    protected void initData() {
        super.initData();
        Bundle bundle = getBundle();

        imcomeList = new ArrayList<>();
        ProductConfigBean.RepaymentTypeBean bean = new ProductConfigBean.RepaymentTypeBean();
        bean.setType("1");
        bean.setName("8000-10000");
        imcomeList.add(bean);

        unitList = new ArrayList<>();
        bean = new ProductConfigBean.RepaymentTypeBean();
        bean.setType("1");
        bean.setName("国有企业");
        unitList.add(bean);
        bean = new ProductConfigBean.RepaymentTypeBean();
        bean.setType("2");
        bean.setName("公私合作");
        unitList.add(bean);
        bean = new ProductConfigBean.RepaymentTypeBean();
        bean.setType("3");
        bean.setName("中外合资");
        unitList.add(bean);
        bean = new ProductConfigBean.RepaymentTypeBean();
        bean.setType("4");
        bean.setName("社会组织");
        unitList.add(bean);
        bean = new ProductConfigBean.RepaymentTypeBean();
        bean.setType("5");
        bean.setName("外资企业");
        unitList.add(bean);
        bean = new ProductConfigBean.RepaymentTypeBean();
        bean.setType("6");
        bean.setName("私营企业");
        unitList.add(bean);
        bean = new ProductConfigBean.RepaymentTypeBean();
        bean.setType("7");
        bean.setName("集体企业");
        unitList.add(bean);

        workList = new ArrayList<>();
        bean = new ProductConfigBean.RepaymentTypeBean();
        bean.setType("1");
        bean.setName("兼职");
        workList.add(bean);
        bean = new ProductConfigBean.RepaymentTypeBean();
        bean.setType("2");
        bean.setName("全职");
        workList.add(bean);
    }

    private void reset() {
        ollIncome.setRightContent("请选择月收入范围");
        ollUnitType.setRightContent("请选择单位性质");
        ollWorkType.setRightContent("请选择工作性质");
    }

    @OnClick({R.id.oll_income,R.id.oll_unit_type, R.id.oll_work_type,R.id.btn_user_suppl_ok})
    public void onViewClicked(View view) {
        int id = view.getId();
        switch (id) {
            case R.id.btn_user_suppl_ok:
                final AlertDialog alertDialog = new AlertDialog.Builder(UserSupplActivity.this)
                        .setTitle("提示")
                        .setMessage("您的信息已经检查完毕，是否立即提交？")
                        .setNegativeButton("取消", new DialogInterface.OnClickListener() {
                            @Override
                            public void onClick(DialogInterface dialog, int which) {
                                dialog.dismiss();
                            }
                        })
                        .setPositiveButton("确认", new DialogInterface.OnClickListener() {
                            @Override
                            public void onClick(DialogInterface dialog, int which) {
                                dialog.dismiss();
                                saveUserSuppl();
                            }
                        }).setCancelable(false).create();
                alertDialog.show();
                break;
            default:
                clickDialog(id);
                break;
        }
    }
    @Override
    public void onItemSelect(IPickerViewData iPickerViewData, int position, int flag) {
        switch (flag) {
            case 0://月收入范围
                ProductConfigBean.RepaymentTypeBean incomeBean = (ProductConfigBean.RepaymentTypeBean) iPickerViewData;
                ollIncome.setRightContent(incomeBean.getName());
                break;
            case 1://单位性质
                ProductConfigBean.RepaymentTypeBean unitTypeBean = (ProductConfigBean.RepaymentTypeBean) iPickerViewData;
                ollUnitType.setRightContent(unitTypeBean.getName());
                break;
            case 2: //工作性质
                ProductConfigBean.RepaymentTypeBean workTypeBean = (ProductConfigBean.RepaymentTypeBean) iPickerViewData;
                ollWorkType.setRightContent(workTypeBean.getName());
                break;
        }
    }

    private void clickDialog(int id) {
        CommonDialog.Builder builder = new CommonDialog.Builder();
        builder.setContext(this).setOnItemSelectListener(this);
        switch (id) {
            case R.id.oll_income://月收入范围
                builder.setData(imcomeList).setFlag(0).setTitle(getString(R.string.payback_income));
                break;
            case R.id.oll_unit_type://单位性质
                builder.setData(unitList).setFlag(1).setTitle(getString(R.string.payback_unit));
                break;
            case R.id.oll_work_type://工作性质
                builder.setData(workList).setFlag(2).setTitle(getString(R.string.payback_work));
                break;
        }
        builder.build().showPickerView();
    }

    private void loanUserSuppl() {
        show();
        UserLoginInfo userLoginInfo = DMApplication.getInstance().getmUserLoginInfo();
        String usid = userLoginInfo.getUserId();
        HttpParams params = new HttpParams();
        params.put("userId", usid);
        DMOKHttpUtils.getInstance().post(this, DMConstant.ApiURL.USERSUPPL_GET, params, new RequestCallBack<UserSupplBean>() {
            @Override
            public void onSuccess(JsonResult<UserSupplBean> response) {
                dismiss();
                if (DMConstant.Code.CODE_OK.equals(response.getCode())) {
                    UserSupplBean bean = (UserSupplBean)response.getObject();
                    etEamil.setTextContent(bean.getEmail());
                    etAddress.setTextContent(bean.getNowAddress());
                    ollIncome.setRightContent(bean.getIncome());
                    ollUnitType.setRightContent(bean.getUnitType());
                    ollWorkType.setRightContent(bean.getWorkType());
                } else {
                    ToastUtil.getInstant().show(UserSupplActivity.this, response.getDescription());
                }
            }

            @Override
            public void onFailure(String msg) {
                dismiss();
                ToastUtil.getInstant().show(UserSupplActivity.this, msg);
            }

            @Override
            public void onConnectFailure(String msg) {
            }
        });
    }

    private void saveUserSuppl() {
        show();
        UserLoginInfo userLoginInfo = DMApplication.getInstance().getmUserLoginInfo();
        HttpParams params = new HttpParams();
        params.put("userId", userLoginInfo.getUserId());
        params.put("email", etEamil.getContent());
        params.put("nowAddress", etAddress.getContent());
        params.put("income", ollIncome.getRightText());
        params.put("unitType", ollUnitType.getRightText());
        params.put("workType", ollWorkType.getRightText());
        DMOKHttpUtils.getInstance().post(this, DMConstant.ApiURL.USERSUPPL_SAVE, params, new RequestCallBack<UserSupplBean>() {
            @Override
            public void onSuccess(JsonResult<UserSupplBean> response) {
                dismiss();
                if (DMConstant.Code.CODE_OK.equals(response.getCode())) {
                    finish();//关闭当前
                } else {
                    ToastUtil.getInstant().show(UserSupplActivity.this, response.getDescription());
                }
            }

            @Override
            public void onFailure(String msg) {
                dismiss();
                ToastUtil.getInstant().show(UserSupplActivity.this, msg);
            }

            @Override
            public void onConnectFailure(String msg) {
            }
        });
    }
}
